<template>
  <div>
  <el-card class="box-card">
    <el-row :gutter="10">
      <el-col :span="15">
        <el-input clearable v-model="queryInfo.query" placeholder="请输入订单编号" prefix-icon="el-icon-search" >
          <el-button slot="append" icon="el-icon-search" @click="getPurchaseList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="9">
        <el-button type="primary" @click="openAddPurchaseDialog" icon = "el-icon-circle-plus" >新增</el-button>
        <el-button type="success" @click="check" icon = "el-icon-success">审核</el-button>
        <el-button type="danger" @click="cancel" icon = "el-icon-error">撤销审核</el-button>
      </el-col>
    </el-row>
    <el-table :data="purchaseList.slice((queryInfo.pageNum-1)*queryInfo.pageSize,queryInfo.pageNum*queryInfo.pageSize)"
    style="width: 100%"
    stripe
    border
    fixed
    height="500"
    @selection-change="handleSelectionChange">
        <el-table-column
          fixed="left"
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="purchaseId"
          label="采购订单编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="product"
          label="商品名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="barCode"
          label="商品条码"
          width="180">
        </el-table-column>
        <el-table-column
          prop="supplier"
          label="供应商名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="count"
          label="商品总数量"
          width="180">
        </el-table-column>
        <el-table-column
          prop="storehouse"
          label="仓库名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          width="180">
        </el-table-column>
        <el-table-column
          fixed="right"
          prop="operate"
          label="操作"
          width="180"
          align="center">
          <template slot-scope="scope" >
            <el-button type="primary" icon="el-icon-edit" size="small" @click="open(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="small" @click="deletePurchase(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

     <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
      </el-pagination>

    </el-card>

    <el-dialog title="添加采购订单" :visible.sync="addPurchaseDialogVisible" width="65%" @close="closeAddPurchaseDialog">
       <el-form :model="purchaseForm" :rules="rules" ref="addPurchaseRuleForm" label-width="100px" class="demo-ruleForm" :inline="true">
         <el-form-item label="订单编号" prop="purchaseId">
           <el-input :disabled="true" v-model="purchaseForm.purchaseId"></el-input>
         </el-form-item>
         <el-form-item label="商品名称" prop="product">
           <el-select v-model="purchaseForm.product" placeholder="请选择"  >
               <el-option
                 v-for="product in productList"
                 :key="product.name"
                 :label="product.name"
                 :value="product.name">
               </el-option>
             </el-select>
         </el-form-item>
         <el-form-item label="供应商" prop="supplier">
           <el-select v-model="purchaseForm.supplier" placeholder="请选择"  >
               <el-option
                 v-for="supplier in supplierList"
                 :key="supplier.name"
                 :label="supplier.name"
                 :value="supplier.name">
               </el-option>
             </el-select>
         </el-form-item>
         <el-form-item label="所属仓库" prop="storehouse">
           <el-select v-model="purchaseForm.storehouse" placeholder="请选择"  >
               <el-option
                 v-for="storehouse in storehouseList"
                 :key="storehouse.name"
                 :label="storehouse.name"
                 :value="storehouse.name">
               </el-option>
             </el-select>
         </el-form-item>
         <el-form-item label="商品数量" prop="count">
            <el-input v-model="purchaseForm.count"></el-input>
         </el-form-item>
       </el-form>

       <div slot="footer" class="dialog-footer">
         <el-button @click="addPurchaseDialogVisible = false">取 消</el-button>
         <el-button type="primary" @click="addPurchase">确 定</el-button>
       </div>
     </el-dialog>

     <el-dialog title="添加采购订单" :visible.sync="addPurchaseDialogVisible" width="65%" @close="closeAddPurchaseDialog">
        <el-form :model="purchaseForm" :rules="rules" ref="addPurchaseRuleForm" label-width="100px" class="demo-ruleForm" :inline="true">
          <el-form-item label="订单编号" prop="purchaseId">
            <el-input :disabled="true" v-model="purchaseForm.purchaseId"></el-input>
          </el-form-item>
          <el-form-item label="商品名称" prop="product">
            <el-select v-model="purchaseForm.product" placeholder="请选择" @change="getProductList">
                <el-option
                  v-for="product in productList2"
                  :key="product.name"
                  :label="product.name"
                  :value="product.name">
                </el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="供应商" prop="supplier">
            <el-select v-model="purchaseForm.supplier" placeholder="请选择"  >
                <el-option
                  v-for="supplier in supplierList"
                  :key="supplier.name"
                  :label="supplier.name"
                  :value="supplier.name">
                </el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="所属仓库" prop="storehouse">
            <el-select v-model="purchaseForm.storehouse" placeholder="请选择"  >
                <el-option
                  v-for="storehouse in storehouseList"
                  :key="storehouse.name"
                  :label="storehouse.name"
                  :value="storehouse.name">
                </el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="商品数量" prop="count">
             <el-input-number v-model="purchaseForm.count" controls-position="right" :min="1"></el-input-number>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="addPurchaseDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addPurchase">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="修改采购订单" :visible.sync="updataPurchaseDialogVisible" width="65%" @close="closeUpdataPurchaseDialog">
         <el-form :model="purchaseForm" :rules="rules" ref="updataPurchaseRuleForm" label-width="100px" class="demo-ruleForm" :inline="true">
           <el-form-item label="订单编号" prop="purchaseId">
             <el-input :disabled="true" v-model="purchaseForm.purchaseId"></el-input>
           </el-form-item>
           <el-form-item label="商品名称" prop="product">
             <el-select v-model="purchaseForm.product" placeholder="请选择"  @change="getProductList">
                 <el-option
                   v-for="product in productList2"
                   :key="product.name"
                   :label="product.name"
                   :value="product.name">
                 </el-option>
               </el-select>
           </el-form-item>
           <el-form-item label="供应商" prop="supplier">
             <el-select v-model="purchaseForm.supplier" placeholder="请选择"  >
                 <el-option
                   v-for="supplier in supplierList"
                   :key="supplier.name"
                   :label="supplier.name"
                   :value="supplier.name">
                 </el-option>
               </el-select>
           </el-form-item>
           <el-form-item label="所属仓库" prop="storehouse">
             <el-select v-model="purchaseForm.storehouse" placeholder="请选择"  >
                 <el-option
                   v-for="storehouse in storehouseList"
                   :key="storehouse.name"
                   :label="storehouse.name"
                   :value="storehouse.name">
                 </el-option>
               </el-select>
           </el-form-item>
           <el-form-item label="商品数量" prop="count">
              <el-input v-model="purchaseForm.count"></el-input>
           </el-form-item>
         </el-form>

         <div slot="footer" class="dialog-footer">
           <el-button @click="updataPurchaseDialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="checkUpdataPurchase">确 定</el-button>
         </div>
       </el-dialog>
  </div>

</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      purchaseList: [],
      addPurchaseDialogVisible: false,
      updataPurchaseDialogVisible: false,
      productList: [],
      productList2: [],
      supplierList: [],
      storehouseList: [],
      selectPurchaseList: [],
      purchaseForm: {
        purchaseId: '',
        product: '',
        barCode: '',
        supplier: '',
        count: '',
        storehouse: '',
        status: ''
      },
      rules: {
        product: [
          { required: true, message: '请输入商品名', trigger: 'blur' }
        ],
        supplier: [
          { required: true, message: '请输入供应商', trigger: 'blur' }
        ],
        count: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        storehouse: [
          { required: true, message: '请输入所属仓库', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.queryInfo.pageSize = val
      this.getPurchaseList()
    },

    handleCurrentChange (val) {
      this.queryInfo.pageNum = val
      this.getPurchaseList()
    },

    async getPurchaseList () {
      const { data: result } = await this.$http.get('/purchase/list', { params: this.queryInfo })
      if (result.status !== 200) return this.$message.error('获取列表失败')
      this.total = result.data.total
      this.purchaseList = result.data.rows
    },

    addPurchase () {
      this.$refs.addPurchaseRuleForm.validate(async validate => {
        if (!validate) return this.$message.error('请填写必填项')
        this.purchaseForm.status = '待审核'
        const { data: result } = await this.$http.post('/purchase/addPurchase', this.purchaseForm)
        if (result.status !== 200) return this.$message.error('添加订单失败')
        this.getPurchaseList()
        this.addPurchaseDialogVisible = false
      })
    },

    async getProductList () {
      const { data: result } = await this.$http.get('/purchase/getProductList', { params: { name: this.purchaseForm.product } })
      if (result.status !== 200) return this.$message.error('获取商品列表失败')
      this.productList = result.data
      this.purchaseForm.barCode = this.productList[0].barCode
    },

    openAddPurchaseDialog () {
      this.addPurchaseDialogVisible = true
      var now = new Date()
      this.purchaseForm.purchaseId = 'IP' + now.getTime()
      console.log('IP' + now.getTime())
      if (this.productList2.length == 0) this.productList2 = this.productList
    },

    async getSupplierList () {
      const { data: result } = await this.$http.get('/purchase/getSupplier')
      if (result.status !== 200) return this.$message.error('获取供应商列表失败')
      this.supplierList = result.data
    },

    async getStorehouseList () {
      const { data: result } = await this.$http.get('/purchase/getStorehouse')
      if (result.status !== 200) return this.$message.error('获取仓库列表失败')
      this.storehouseList = result.data
    },

    closeUpdataPurchaseDialog () {
      this.$refs.updataPurchaseRuleForm.resetFields()
    },

    closeAddPurchaseDialog () {
      this.$refs.addPurchaseRuleForm.resetFields()
    },

    async deletePurchase (purchase) {
      if (purchase.status !== '待审核') return this.$message.error('订单' + purchase.purchaseId + '进行中')
      const result = await this.$confirm('此操作将永久删除订单' + purchase.purchaseId + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (result !== 'confirm') return this.$message.info('用户取消操作')
      const { data: resultDB } = await this.$http.delete(`/purchase/delete/${purchase.purchaseId}`)
      if (resultDB.status !== 200) return this.$message.error('删除订单失败')
      this.getPurchaseList()
      this.$message.success('订单删除成功')
    },

    open (purchase) {
      if (purchase.status !== '待审核') return this.$message.error('订单' + purchase.purchaseId + '进行中')
      this.purchaseForm = purchase
      this.updataPurchaseDialogVisible = true
      if (this.productList2.length == 0) this.productList2 = this.productList
    },

    checkUpdataPurchase () {
      this.$refs.updataPurchaseRuleForm.validate(validate => {
        if (!validate) return this.$message.error('请填写必填项')
        this.updataPurchase(this.purchaseForm)
        this.$message.success('订单修改成功')
        this.updataPurchaseDialogVisible = false
      })
    },

    async updataPurchase (purchase) {
      const { data: result } = await this.$http.put('/purchase/updata', purchase)
      if (result.status !== 200) return this.$message.error('修改订单失败')
      this.getPurchaseList()
    },

    handleSelectionChange (val) {
      this.selectPurchaseList = val
    },

    check () {
      for (var i = 0; i < this.selectPurchaseList.length; i++) {
        if (this.selectPurchaseList[i].status === '待审核') {
          this.selectPurchaseList[i].status = '已审核'
          this.updataPurchase(this.selectPurchaseList[i])
          this.$message.success('订单' + this.selectPurchaseList[i].purchaseId + '审核成功')
        } else if (this.selectPurchaseList[i].status === '已审核') {
          this.$message.error('订单' + this.selectPurchaseList[i].purchaseId + '已审核')
        } else {
          this.$message.error('订单' + this.selectPurchaseList[i].purchaseId + '进行中')
        }
      }
    },

    cancel () {
      for (var i = 0; i < this.selectPurchaseList.length; i++) {
        if (this.selectPurchaseList[i].status === '已审核') {
          this.selectPurchaseList[i].status = '待审核'
          this.updataPurchase(this.selectPurchaseList[i])
          this.$message.success('订单' + this.selectPurchaseList[i].purchaseId + '撤销审核成功')
        } else if (this.selectPurchaseList[i].status === '待审核') {
          this.$message.error('订单' + this.selectPurchaseList[i].purchaseId + '未审核')
        } else {
          this.$message.error('订单' + this.selectPurchaseList[i].purchaseId + '进行中')
        }
      }
    }

  },

  mounted () {
    this.getPurchaseList()
    this.getProductList()
    this.getSupplierList()
    this.getStorehouseList()
  }
}
</script>

<style>

</style>
